$(function(){
    //加载风险区域
    queryRegionList();
    /**
     * 保存按钮事件
     */
    $('#btn_save').on("click", function () {
        //var ret = $("#hdRegisterForm").form('validate');

        if($("#pointForm").form('validate')){
            layer.confirm("确定保存？",{btn:['确定','取消']},function(){
                var layerIndex=parent.layer.getFrameIndex(window.name); //获取窗口索引
                //序列化表单数据
                var data = $("#pointForm").serializeArray();
                //data.push({name:'precautionData',value:getPrecautionData()});
                //console.log(data);
                $.ajax({
                    url:ROOT_PATH + '/risk/region/addRegionPointInfo.shtml',//提交地址
                    data:data,//将表单数据序列化
                    type:"POST",
                    dataType:"json",
                    success:function(result){
                        if (result.success){
                            layer.msg(result.message,{time:1000,icon:1},function(){
                                WindowManager.closeLayer({
                                    //returnValue:""
                                });
                            });
                        }else{
                            layer.msg(result.message,{time:1000,icon:1},function(){
                                //parent.layer.close(layerIndex);
                            });
                        }
                    }
                });
            });
        }else{
            layer.msg("请按规则填写信息!",{time:1000,icon:2});
        }

    });
});
/**
 * 定义表单验证
 */
$.extend($.fn.validatebox.defaults.rules, {
    integer: {// 验证非负整数
        validator: function (value) {
            //return /^[+]?[1-9]+\d*$/i.test(value);
            return  /^\d+$/.test(value);
        },
        message: '请输入整数'
    },
    validSelect:{//验证为空字符串
        validator:function(value){
            //alert(value!="请选择");
            return value!="请选择"&&value!='';
        },
        message:'请选择'
    },
    validRegionSelect:{//验证为空字符串
        validator:function(value,param){
            var val = $(param[0]).val();
            //alert(value!="请选择");
            return val!='';
        },
        message:'请选择'
    }

});
/**
 * 查询风险区域列表
 */
function queryRegionList(){
    $.ajax({
        url:ROOT_PATH + '/risk/region/queryComboboxList.shtml',//提交地址
        data:{},
        type:"POST",
        dataType:"json",
        success:function(result){
            $('#region').combobox(
                {
                    data:result,
                    valueField:'id',
                    textField:'text',
                    /*loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        for(var i=0;i<data.length;i++){
                            newData.push(data[i])
                        }
                        return newData;
                    },*/
                    onSelect:function (param) {
                        $('#regionId').val(param.id);
                    },
                    onChange:function(newVal,oldVal){
                        //区分选中还是手动输入
                        if(newVal.length !=32){
                            $('#regionId').val('');
                        }
                    }
                }
            );

        }
    });
}
